package org.opnlp.opnlp2.io;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import org.opnlp.opnlp2.model.Collection;
import org.opnlp.opnlp2.model.Document;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.opnlp.opnlp2.settings.Configuration;

/**
 * Generates JSON File from documents loaded from csv file.
 * @author Stefan
 * @version 1.0
 */
public class JSONGenerator {
    
    
    /**
     * Generates JSON File from collection of loaded documents.
     * @throws JSONException 
     * @author Stefan
     * @version 1.0
     */
    public static void generateJSONFile () throws JSONException{
        
        JSONObject mainObject = new JSONObject();
        JSONArray mainJsonArray = new JSONArray();
        int counter = 1;
        for (Document doc : Collection.instance.collectionOfDocuments) {
            JSONArray jsonArray = new JSONArray();
            jsonArray.put(doc.getIdFromRDB());
            jsonArray.put(doc.getText());
            jsonArray.put(doc.getMpName());
            jsonArray.put(doc.getDate());
            mainJsonArray.put(jsonArray);
            counter ++;
        }
        mainObject.put("Obracanje poslanika", mainJsonArray);
        System.out.println(mainObject);
        
        try{
        
        File file = new File(Configuration.locationOfExportedJSONFile);
	BufferedWriter writer = new BufferedWriter(new FileWriter(file));
			
	JSONWriter jsonWriter = new JSONWriter(writer);
			
	jsonWriter.object().key("Obracanje poslanika").value(mainJsonArray).endObject();
	writer.close();
	System.out.println("Text is writed in file.");
	} catch (Exception e) {
		e.printStackTrace();
	}
    }
}
